using System;
using System.Xml;

namespace Umbraco.Ecommerce.BusinessLogic
{
	/// <summary>
	/// Summary description for OrderLine.
	/// </summary>
	public class OrderLine 
	{
		private string _ProductId;
		private string _ProductName;
		private int _Quantity;
		private decimal _Price;
		
		public string ProductId
		{
			get {  return _ProductId; }
			set { _ProductId = value; }    
		}
	
		public string ProductName
		{
			get {  return _ProductName; }
			set { _ProductName = value; }    
		}

		public int Quantity
		{
			get {  return _Quantity; }
		    set { _Quantity = value; }    
		}

		public decimal Price
		{
			get {  return _Price; }
			set { _Price = value; }    
		}
		
		public static OrderLine FromXml(string Xml) {
				XmlDocument d = new XmlDocument();
				d.LoadXml(Xml);
				OrderLine o = new OrderLine();
				o.Price = decimal.Parse(d.SelectSingleNode("/orderline/price").InnerText, Store.InputNumberFormatInfo);
				o.ProductId = d.SelectSingleNode("/orderline/productId").InnerText;
				o.ProductName = d.SelectSingleNode("/orderline/productName").InnerText;
				o.Quantity = int.Parse(d.SelectSingleNode("/orderline/quantity").InnerText);
				return o;
		}

		public static OrderLine FromXml(XmlNode n) 
		{
			try 
			{
				OrderLine o = new OrderLine();
				o.Price = decimal.Parse(n.SelectSingleNode("price").InnerText, Store.InputNumberFormatInfo);
				o.ProductId = n.SelectSingleNode("productId").InnerText;
				o.ProductName = n.SelectSingleNode("productName").InnerText;
				o.Quantity = int.Parse(n.SelectSingleNode("quantity").InnerText);
				return o;
			}
			catch {
			
			}
			return null;
		}

		public XmlNode toXml(XmlDocument d) {
			XmlNode n = d.CreateElement("orderline");
			

			XmlElement price = d.CreateElement("price");
			price.AppendChild(d.CreateTextNode(Price.ToString(Store.InputNumberFormatInfo)));
			
			XmlElement qty = d.CreateElement("quantity");
			qty.AppendChild(d.CreateTextNode(Quantity.ToString()));
			
			XmlElement productName = d.CreateElement("productName");
			productName.AppendChild(d.CreateTextNode(ProductName.ToString()));
			
			XmlElement productId = d.CreateElement("productId");
			productId.AppendChild(d.CreateTextNode(ProductId.ToString()));

			XmlElement total = d.CreateElement("total");
			total.AppendChild(d.CreateTextNode((Price*Quantity).ToString(Store.InputNumberFormatInfo)));
			
			
			n.AppendChild(price);
			n.AppendChild(qty);
			n.AppendChild(productName);
			n.AppendChild(productId);
			n.AppendChild(total);

			return n;
		}

	}
}
